home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
byte0487.arc
/
GREHAN.ARC
/
SIEVE386.ASM
< prev
Wrap
Assembly Source File
|
1986-11-23
|
3KB
|
197 lines
; 80386 assembly version of sieve program.
; Prints out the number of primes found in hexadecimal.
; (Printing out the number of primes found in decimal
; is left as an exercise for the reader.)
;
; First define constants
ITERVAL equ 50 ;Iteration count
TRUE equ 1
FALSE equ 0
ASIZE equ 8190 ;Array size
assume cs:sieve,ds:sdata
sieve segment para public use32 'code'
public _start_
;
; *** ENTRY POINT ***
_start_ proc near
;
; Tell user we are beginning the program
;
lea ebx,startmsg
call printmsg
;
; Setup loop counter
mov ax,ITERVAL ;Number of iterations
mov iter,ax
;
; Initialize counter for number of primes
L0: xor eax,eax
mov count,ax
;
; Set all flags true
lea ebx,flags
mov ecx,ASIZE+1
L1: mov byte ptr[ebx],TRUE
inc ebx
loop L1
;
; Primary loop
mov index,eax
L2: mov ebx,index
mov al,flags[ebx]
or al,al ;Is it a prime?
jz L5
;
; Found a prime
mov eax,ebx ;Twice index plus 3
add eax,ebx
add eax,3
;
; Kill all multiples
L3: add ebx,eax
cmp ebx,ASIZE
jg L4
mov byte ptr flags[ebx],FALSE
jmp L3
;
; Count number of primes
L4: inc word ptr count
;
L5: inc dword ptr index
cmp dword ptr index,ASIZE+1
jne L2
;
; Do another iteration
dec word ptr iter
jnz L0
;
; Print out number of primes
; For now, in hex
lea ebx,npmsg
call printmsg
mov ax,count
call hwout
;
; Terminate process
mov ax,04C00h
int 21H
_start_ endp
;
; THE FOLLOWING CODE WAS INCLUDED WITH THE PHAR LAP 386ASM/
; 386LINK PACKAGE AS PART OF DEMO SOFTWARE.
;
; printmsg - Print a message to the screen
;
; ebx - Points to the message to be printed out. The message
; must be null terminated.
;
printmsg proc near
push edx ; Save EDX.
pm1: mov dl,[ebx] ; Load the next character into DL and
or dl,dl ; branch if null.
je pm3 ;
mov ah,02h ; Output the character.
int 21h ;
cmp byte ptr [ebx],0ah ; If the character is a LF, then
jne pm2 ; also output a CR.
mov ah,02h ;
mov dl,0dh ;
int 21h ;
pm2: add ebx,1 ; Increment the message pointer and
jmp pm1 ; loop.
pm3: pop edx ; Restore EDX and return.
ret ;
printmsg endp ;
;
; hwout - Output the hex word in AX to the screen
;
hwout proc near ;
push ax ; Output the high byte of the word.
ror ax,8 ;
call hbout ;
pop ax ; Output the low byte of the word.
call hbout ;
ret ; Return.
hwout endp ;
;
; hbout - Output the hex byte in AL to the screen
;
hbout proc near ;
push ax ; Output the high digit of the byte.
ror ax,4 ;
call hdout ;
pop ax ; Output the low digit of the byte.
call hdout ;
ret ; Return.
hbout endp
;
; hdout - Output the hex digit in AL to the screen
;
hdout proc near
and ax,0fh ; Zap any extra bits and translate
cmp ax,10 ; to ASCII.
jg hd1 ;
add al,'0'-'A'+10 ;
hd1: add al,'A'-10 ;
push dx ; Call MS-DOS to output the digit.
mov dl,al ;
mov ah,2h ;
int 21h ;
pop dx ;
ret ; Return.
hdout endp
sieve ends
sdata segment para public use32 'data'
startmsg db 'Beginning sieve.',0dh,0ah,0
npmsg db 'Number of primes:',0
flags db ASIZE+1 dup (?)
index dd ?
iter dw ?
count dw ?
sdata ends
_stack segment byte stack use32 'stack'
db 8000 dup (?)
_stack ends
end